//
//  CXHViewController.m
//  CXHAdSDK
//
//  Created by 华资融创 on 06/06/2022.
//  Copyright (c) 2022 华资融创. All rights reserved.
//

#import "CXHViewController.h"
#import "CXHBannerViewController.h"
#import "CXHInterstitialViewController.h"
#import "CXHNativeExpressViewController.h"
#import "CXHUnStandardViewController.h"
#import "CXHServiceAdViewController.h"
#import "CXHSplashViewController.h"
#import "CXHAdSDK-Core/CXHAdSDK.h"
#import "CXHAdSDK-Core/CXHAdSDKBannerAdView.h"
#import <CXHAdSDK-Adapter/CXHAdSDKAdapter.h>
#import <CXHAdSDK-Kit/CXHUtilsKit.h>

@interface CXHViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *mainTableView;

@property (nonatomic, strong) NSArray *dataArray;

@end

@implementation CXHViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    
    self.title = [NSString stringWithFormat:@"CXHAdSDK Demo V%@",[CXHAdSDK getSDKVersion]];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.dataArray = @[@"banner",@"插屏", @"信息流模板", @"非标准广告", @"开屏广告"];
    
    [self.view addSubview:self.mainTableView];
    
    CXHChannelInfo *channelInfo = [[CXHChannelInfo alloc]init];
    // bwt上报需要设置
    channelInfo.userID = @"123";
    channelInfo.key = @"MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCzr4T+4bnyYnamk2CPuhU08CyttEnuaYr7hSC1jZvL+i1o9l66wRCeP9z5GonsT6kH9hN0KBnJEfsgYvv9yDi0qSk4qYqMOPimlWRELMcqNw9UXihL3mmvdKio+Q+Y7lyx51kDR3a0TWfkMkgrif4HsUqQNCKDJ04+6oo0+BorcQIDAQAB";
    channelInfo.platformType = CXHAdapterPlatformBWT;
    channelInfo.baseUrl = @"https://dev.bwton.cn/bsp/dev/bas/ad/v1/advertisement/getAds";
    [CXHAdSDK setChannelInfoList:@[channelInfo]];
    
    [CXHAdSDK setLogLevel:CXHSDKLogLevelDebug];
    // 设置停止广告请求
//    [CXHAdSDK setNoAd];
    
    // 域名设置需要在SDK初始化前
//    [CXHAdSDK setRequestDomain:@"http://ad.shunchangzhixing.com"];
    
    // 1303 978472
    [CXHAdSDK initWithAppId:@"1303" completionBlock:^(NSError * _Nullable error) {
        if (error) {
            NSLog(@"初始化失败:%@", error);
        }
    }];
    
    [self.mainTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];

}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    //白色
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    //黑色
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
}

#pragma mark - UITableViewDelegate,UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    UIView *view = [cell.contentView viewWithTag:999];
    if (view) {
        [view removeFromSuperview];
    }
    UILabel *labTitle = [[UILabel alloc]init];
    labTitle.font = [UIFont systemFontOfSize:20];
    labTitle.textColor = [UIColor blackColor];
    labTitle.tag = 999;
    labTitle.text = [self.dataArray objectAtIndex:indexPath.row];
    [cell.contentView addSubview:labTitle];
    labTitle.frame = CGRectMake(19, 15, [UIScreen mainScreen].bounds.size.width - 38, 50);
    
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 80;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    if (indexPath.section == 0) {
        switch (indexPath.row) {
            case 0:
            {
                [self loadBanner];
            }
                break;
            case 1:
            {
                [self loadCXHAdSDKInterstitial];
            }
                break;
            case 2:{
                [self  loadNativeExpress];
            }
            case 3:{
                [self loadUnStandardAd];
            }
            case 4:
                [self loadSplashAd];
            default:
                break;
        }
    }
}

- (UITableView *)mainTableView{
    if (!_mainTableView) {
        _mainTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height) style:UITableViewStylePlain];
        _mainTableView.delegate = self;
        _mainTableView.dataSource = self;
        _mainTableView.backgroundColor = [UIColor whiteColor];
        
        
    }
    return _mainTableView;
}

#pragma mark -

- (void) loadBanner {
    CXHBannerViewController *vc = [CXHBannerViewController new];
    UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:vc];
    nav.modalPresentationStyle = UIModalPresentationFullScreen;
    [self presentViewController:nav animated:YES completion:nil];
}

- (void)loadCXHAdSDKInterstitial{
    CXHInterstitialViewController *vc = [CXHInterstitialViewController new];
    UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:vc];
    nav.modalPresentationStyle = UIModalPresentationFullScreen;
    [self presentViewController:nav animated:YES completion:nil];
}

- (void)loadNativeExpress{
    CXHNativeExpressViewController *vc = [CXHNativeExpressViewController new];
    UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:vc];
    nav.modalPresentationStyle = UIModalPresentationFullScreen;
    [self presentViewController:nav animated:YES completion:nil];
}

- (void)loadUnStandardAd{
    CXHServiceAdViewController *vc = [CXHServiceAdViewController new];
    UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:vc];
    nav.modalPresentationStyle = UIModalPresentationFullScreen;
    [self presentViewController:nav animated:YES completion:nil];
}

- (void)loadSplashAd{
    CXHSplashViewController *splashVC = [CXHSplashViewController new];
    UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:splashVC];
    nav.modalPresentationStyle = UIModalPresentationFullScreen;
    [self presentViewController:nav animated:YES completion:nil];
}

@end
